import game
import copy

allID=set(range(1,13))

def allToID(allBox):
    ret=[]
    for i in allBox:
        ret.append(i.ID)
    return set(ret)

def getMove(g:game.game):
    def getT1Move(g:game.game):
        allMove=[]
        # 枚举移动
        for i in g.t1.aroundBuilding:
            if i!=g.t1.nowPos:
                # 尝试移动
                newg=copy.copy(g)
                ret=newg.t1.moveTo(i)
                if ret!=False:
                    allMove.append(ret)
        # 枚举装卸
        for i in g.t1.allBox: # 在车里的
            newg=copy.copy(g)
            ret=newg.t1.unloadBox(i.ID)
            if ret!=False:
                allMove.append(ret)
        for i in allID-allToID(g.t1.allBox): # 没在车里的
            newg=copy.copy(g)
            ret=newg.t1.loadBox(i)
            if ret!=False:
                allMove.append(ret)

        return allMove

    if g.t1.isStop():
        allMove=getT1Move(g)
    else:
        allMove=[]

    def getTAMove(g:game.game):
        allMove=[]
        # 枚举移动
        for i in g.tA.aroundBuilding:
            if i!=g.tA.nowPos:
                # 尝试移动
                newg=copy.copy(g)
                ret=newg.tA.moveTo(i)
                if ret!=False:
                    allMove.append(ret)
        # 枚举装卸
        if g.tA.nowPos==g.tA.aroundBuilding[2]:
            for i in g.tA.allGroup:  # 在车里的
                newg = copy.copy(g)
                ret = newg.tA.unload(i.ID)
                if ret != False:
                    allMove.append(ret)
            for i in allID - allToID(g.tA.allGroup):  # 没在车里的
                newg = copy.copy(g)
                ret = newg.tA.load(i)
                if ret != False:
                    allMove.append(ret)
        else:
            # 枚举From/To Box
            for i in g.tA.nowPos.allBox: # 在建筑里的
                newg = copy.copy(g)
                ret = newg.tA.loadFromBox(i.ID)
                if ret != False:
                    allMove.append(ret)
            for i in g.tA.allGroup: # 在车里的
                newg = copy.copy(g)
                ret = newg.tA.unloadToBox(i.ID)
                if ret != False:
                    allMove.append(ret)

        return allMove

    if g.tA.isStop():
        allMove+=getTAMove(g)

    def getTBMove(g:game.game):
        allMove=[]
        # 枚举移动
        for i in g.tB.aroundBuilding:
            if i!=g.tB.nowPos:
                # 尝试移动
                newg=copy.copy(g)
                ret=newg.tB.moveTo(i)
                if ret!=False:
                    allMove.append(ret)
        # 枚举装卸
        for i in g.tB.allGroup:  # 在车里的
            newg = copy.copy(g)
            ret = newg.tB.unload(i.ID)
            if ret != False:
                allMove.append(ret)
        for i in allID - allToID(g.tB.allGroup):  # 没在车里的
            newg = copy.copy(g)
            ret = newg.tB.load(i)
            if ret != False:
                allMove.append(ret)

        return allMove

    if g.tB.isStop():
        allMove+=getTBMove(g)

    return allMove